package com.jt.controller;

import com.alibaba.druid.support.logging.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.pojo.BasePojo;
import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.util.RedisUtil;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.HttpCookie;
import java.util.*;

/**
 * @author 刘昱江
 * 时间 2021/5/11
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    private UserService userService;

    //redis的数据存取与获取
    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/hello")
    public List<User> hello() {
        return userService.findAll();
    }

    /**
     * 需求:根据用户名和密码实现灯枯,要求返回token
     * @param user
     * @return
     */
    @PostMapping("/login")
    public SysResult login(@RequestBody User user,HttpServletResponse response) {
        String   token=userService.login(user);
        if(StringUtils.hasLength(token)){
            ValueOperations valueOperations = redisTemplate.opsForValue();
            valueOperations.set(token, "value");



            return  SysResult.success(token);
        }else {
            return  SysResult.fail();
        }
    }

    //获取用户列表的信息
    @GetMapping("/list")
    public  SysResult  getUserList(PageResult  pageResult){
        PageResult userPage = userService.getUserPage(pageResult);
        return  SysResult.success(userPage);
    }

    @PostMapping("/addUser")
    public  SysResult  addUser(@RequestBody User  user){
        System.out.println(user);
        boolean b = userService.addUser(user);
        if (b)
            return  SysResult.success();
        else
            return SysResult.fail();
    }

    @DeleteMapping("/{id}")
    public  SysResult  delete(@PathVariable Integer id){
         userService.deleteUser(id);
         return  SysResult.success();
    }

    @PutMapping("/updateUser")
    public SysResult  update(@RequestBody User user){
        userService.updateUser(user);
        return   SysResult.success();
    }

    //跟新状态
    @PutMapping("/status/{id}/{status}")
    public  SysResult  updatestatus(@PathVariable("id") Integer id,@PathVariable("status") Boolean status){
        User user = new User().setId(id).setStatus(status);
        userService.updateUser(user);
        return  SysResult.success();
    }


}